﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Configuration;
using System.IO;
using System.Data;


public partial class CMS_View : System.Web.UI.Page
{
    Usas.Model.Cms.cmsArticle artm = new Usas.Model.Cms.cmsArticle();
    Usas.Common.Cms.Role ro = new Usas.Common.Cms.Role();
    Usas.Common.Cms.CurrentInfo cinfo = new Usas.Common.Cms.CurrentInfo();
    Usas.Common.Cms.FileControl fc = new Usas.Common.Cms.FileControl();
    Usas.BLL.Cms.cmsArticle art = new Usas.BLL.Cms.cmsArticle();
    Usas.BLL.Cms.UsasDB udb = new Usas.BLL.Cms.UsasDB();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            try
            {
                Bind();

                MasterPage_MasterPage_CMS mmc = (MasterPage_MasterPage_CMS)this.Page.Master;
                mmc.ManagerHover();
                mmc.LeftExist = false;

            }
            catch
            {
            }
            finally
            {
                ReViewBox1.Visible = false;
            }
        }
    }

    private void Bind()
    {
        try
        {
            object artid = Request["id"];
            if (artid.ToString() != "")
            {

                //更新点击
                art.UpHits(Convert.ToInt32(artid));

                string edit = Request["edit"].ToString();
                string view = Request["view"].ToString();
                string check = Request["check"].ToString();

                //保密
                bool adit = false;

                //验证是否正确访问
                if (ro.GetMd5Str(artid.ToString() + "edit" + "True") == edit)
                {
                    ViewControlBar1.Visible = true;
                    adit = true;
                }
                else
                {
                    if (ro.GetMd5Str(artid.ToString() + "view" + "True") == view)
                    {
                        ViewControlBar1.Visible = false;
                    }
                    else
                    {
                        ContentDiv.InnerHtml = "<div style='text-algin:center'><h1>非法的访问</h1></div>";
                        ReViewBox1.Visible = false;
                        return;
                    }
                }

                //编辑权限
                if (ro.GetMd5Str(artid.ToString() + "check" + "True") == check)
                {
                    check = "1";
                }
                else
                {
                    check = "0";
                }

                artm = art.GetModel(Convert.ToInt32(artid));

                //添加cookie
                if (artm.ModfiyTime.Value.AddDays(15) >= DateTime.Now && Request.Cookies["a_" + artm.ArtId] == null)
                {
                    Request.Cookies.Add(new HttpCookie("a_" + artm.ArtId, "1"));

                    HttpCookie cookie = new HttpCookie("a_" + artm.ArtId, "1");
                    cookie.Expires = DateTime.Now.AddDays(15);
                    Response.AppendCookie(cookie);
                }

                //编辑按钮初始化
                ViewControlBar1.IntiControl(true, artm.ArtId.ToString(), artm.ArtTopic, check);

                //导航条
                RouterBar1.IntiBar(CMS_Control_RouterBar.ShowMode.Article, Convert.ToInt32(artid), new ArrayList());

                lblTname.Text = artm.ArtTopic;

                hits.Text = "　｜　" + "阅览数：" + artm.hits.ToString();

                if (artm.Writer == 0 || artm.Writer == null)
                {
                    writer.Visible = false;
                }
                else
                {
                    DataSet zuozheds = udb.SelEmployees("EmployeeId=" + artm.Writer);
                    writer.Text = zuozheds != null && zuozheds.Tables[0].Rows.Count > 0 ? "　｜　作者：" + zuozheds.Tables[0].Rows[0]["EmpName"].ToString() : "";

                    if (cinfo.GetEmpId() == artm.Writer.Value.ToString())
                    {
                        adit = true;
                    }
                }

                if (artm.Modify == 0 || artm.Modify == null)
                {
                    modfiy.Visible = false;
                }
                else
                {
                    DataSet zuozheds = udb.SelEmployees("EmployeeId=" + artm.Writer);
                    modfiy.Text = zuozheds != null && zuozheds.Tables[0].Rows.Count > 0 ? "　｜　文章录入：" + zuozheds.Tables[0].Rows[0]["EmpName"].ToString() + "　｜　" : "";
                }

                if (artm.ModfiyTime.ToString() == "")
                {
                    modfiytime.Visible = false;
                }
                else
                {
                    modfiytime.Text = "　｜　" + "更新时间：" + artm.ModfiyTime.Value.ToString("yyyy-MM-dd");
                }



                if (artm.ArtSummary.Trim().Length > 0)
                {
                    sum.Visible = true;
                    sumcontent.InnerText = "内容摘要：" + artm.ArtSummary.Trim();
                }
                else
                {
                    sum.Visible = false;
                }

                if (artm.ArtKeys.Trim().Length > 0)
                {
                    artkey.Visible = true;
                    artkey.InnerHtml = "关键字：<span style='font-weight:bold;color:red'>" + artm.ArtKeys.Trim() + "</span>";
                }
                else
                {
                    artkey.Visible = false;
                }

                //设置版本
                Version1.GetValue(artm.ArtId.ToString(), false);

                string temppath = ConfigurationSettings.AppSettings["UsasInformationPath"];
                if (temppath.IndexOf(":") == -1)
                {
                    temppath = MapPath(temppath + "CMSUpLoadFiles\\" + artm.ArtDirectory + "\\" + artm.ArtFileName);
                }
                else
                {
                    temppath += "CMSUpLoadFiles\\" + artm.ArtDirectory + "\\" + artm.ArtFileName;
                }

                adit = (adit ? adit : !artm.Audit);

                if (!adit)
                {
                    ClientScript.RegisterStartupScript(GetType(), "Msg", "NoAudit()", true);
                }

                if (artm.ArtFormat != 2)
                {
                    //lb.Text = "点击此处";
                    FileInfo fn = new System.IO.FileInfo(temppath);

                    fc.OnlySee(pn, true, artm.ArtDirectory.Replace("\\", "/") + "/" + artm.ArtFileName, Path.GetExtension(temppath), adit);

                    FileContent.Visible = false;
                    Plus.Visible = true;
                }
                else
                {
                    if (File.Exists(temppath))
                    {
                        string msg = File.ReadAllText(temppath, System.Text.Encoding.GetEncoding("UTF-8")).ToString();
                        FileContent.InnerHtml = msg;
                        // lb.Visible = false;
                        Plus.Visible = false;
                    }
                    else
                    {
                        FileContent.Visible = false;
                        // lb.Visible = false;
                        Plus.Visible = false;
                    }
                }

                //留言初始化
                ReViewBox1.ChatBoxInit(Convert.ToInt32(artm.ArtId), artm.ArtTopic, check);
            }

        }
        catch
        {
            ViewControlBar1.IntiControl(false, "", "", "");
            ContentDiv.Visible = false;
        }
    }



}
